package com.ksyzt.ksite2.client.log;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.common.PageNaviBar;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.PageLoggerData;
import com.ksyzt.ksite2.shared.SysConst;
import com.ksyzt.ksite2.shared.modle.LOGObj;

public class LogList extends MessageComposite {

	private static LogListUiBinder uiBinder = GWT.create(LogListUiBinder.class);

	interface LogListUiBinder extends UiBinder<Widget, LogList> {
	}

	DateTimeFormat df = DateTimeFormat.getFormat("yyyy年MM月dd日 HH:mm:ss");
	private AsyncCallback<PageLoggerData> m_data = new AsyncCallback<PageLoggerData>() {

		@Override
		public void onSuccess(PageLoggerData data) {

			navigator
					.setData(data.currentPage, (int) data.total, data.pagesize);

			grid.resize(data.logs.size() + 1, 4);
			grid.setCellPadding(3);
			grid.setCellSpacing(0);
			int row = 0;
			int col = 0;
			CellFormatter cf = grid.getCellFormatter();

			cf.setWidth(row, col, "50px");
			grid.setText(row, col++, "类别");

			cf.setWidth(row, col, "200px");
			grid.setText(row, col++, "时间");

			cf.setWidth(row, col, "80px");
			grid.setText(row, col++, "操作员");
			grid.setText(row, col++, "内容");
			row++;
			for (LOGObj log : data.logs) {
				col = 0;
				grid.setText(row, col++, getLevel(log.getLEVEL()));
				grid.setText(row, col++,
						df.format(new Date(log.getADDTIME().getTime())));
				grid.setText(row, col++, log.getAUTHOR());
				grid.setText(row, col++, log.getINFO());
				row++;
			}
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}
	};

	private String getLevel(int level) {
		String info = "";
		switch (level) {
		case 0:
			info = "信息";
			break;
		case 1:
			info = "错误";
			break;
		default:
			info = "未知";
		}
		return info;
	}

	private MessageHandler m_navi_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.PAGE) {
				message(SysConst.MSG_QUERY_DATA);
				PageNaviBar.NaviData data = (PageNaviBar.NaviData) value;
				ServerProxy.SYS_MANAGER.getLogger(data.page, data.size, m_data);
			}
		}
	};

	public LogList() {
		initWidget(uiBinder.createAndBindUi(this));
		navigator.addMessageHandler(m_navi_handler);
	}

	public void init() {
		ServerProxy.SYS_MANAGER.getLogger(0, 20, m_data);
	}

	@UiField
	PageNaviBar navigator;

	@UiField
	Grid grid;

}
